/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.messaging.inbox;

import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import nl.cloudfarming.client.messaging.Message;

/**
 * Panel which represents a message inbox. On the left there is a list of messages. on the right there is a panel which will display the messages in detail
 * On select of one of the messages the panel is updated.
 * @author Merijn Zengers
 */
public class MessageInboxPanel extends javax.swing.JPanel implements PropertyChangeListener {

    private MessageListPanel inboxPanel;
    private MessageDetailPanel messageDetailPanel;

    /** Creates new form MessageInboxPanel */
    public MessageInboxPanel() {
        initComponents();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     */
    private void initComponents() {
        this.setLayout(new BorderLayout());
        inboxPanel = new MessageListPanel();

        messageDetailPanel = new MessageDetailPanel();

        inboxPanel.addMessagePropertyChangeListener(this);


        this.add(inboxPanel, BorderLayout.LINE_START);
        this.add(messageDetailPanel, BorderLayout.CENTER);


    }

    //TODO: make sortable and get it on the right spot. Maybe even make sortable on multiple properties not only date
    public void addMessage(Message message) {
        inboxPanel.addMessage(message);
    }

    /**
     * sets the text of the selected message in the mesage panel
     * @param evt 
     */
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        Message message = inboxPanel.getSelectedInboxRow().getMessage();
        if (message != null) {
            messageDetailPanel.setMessageText(message.getMessageText());
        }
    }
}
